﻿

''' <summary>
''' The lexical defintion of a token. 
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class LexicalToken

    ''' <summary>
    ''' Gets/sets the priority of a lexcal token. The priority is used to resolve ambiguities in the lexical scanning. 
    ''' Lower priority value means higher priority.
    ''' The token of lexcion with lower priority value will be considered fisrt.  
    ''' </summary>
    ''' <value>The value of priority.</value>
    Public Property Priority As Integer

    Private m_regex As RegularExpression
    Public ReadOnly Property RegularExpression() As RegularExpression
        Get
            Return m_regex
        End Get
    End Property

    Private m_tokenTypeIndex As Integer
    Public ReadOnly Property TokenTypeIndex() As Integer
        Get
            Return m_tokenTypeIndex
        End Get
    End Property

    Private m_isSkipToken As Boolean
    Public ReadOnly Property IsSkipToken() As Boolean
        Get
            Return m_isSkipToken
        End Get
    End Property

    Friend Sub New(ByVal regularExpression As RegularExpression, ByVal tokenTypeIndex As Integer, ByVal isSkipToken As Boolean, ByVal priority As Integer)
        m_regex = regularExpression
        Me.Priority = priority
        m_tokenTypeIndex = tokenTypeIndex
        m_isSkipToken = isSkipToken
    End Sub

    Friend Function GenerateNFA() As NFAModel
        Dim nfa = NFAConverter.Default.Convert(RegularExpression)
        Debug.Assert(nfa.EntryEdge IsNot Nothing)
        Debug.Assert(nfa.TailState IsNot Nothing)

        'set token and priority
        nfa.TailState.TokenTypeIndex = m_tokenTypeIndex
        nfa.TailState.Priority = Priority
        nfa.SetEdgePriority(Priority)

        Return nfa
    End Function
End Class